package com.gxa.modules.service.impl;

import com.gxa.common.base.RespResult;
import com.gxa.modules.dto.zw.FindGoodsSaleVO;
import com.gxa.modules.mapper.AfterSaleMapper;
import com.gxa.modules.service.AfterSaleService;
import lombok.SneakyThrows;
import org.apache.shiro.io.ResourceUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.util.*;

/*
 *@Author:那我也
 *@Date:2023/3/24-10:46
 *@Description:
 */
@Service
@MultipartConfig
public class AfterSaleServiceImp implements AfterSaleService {

    @Autowired
    AfterSaleMapper afterSaleMapper;

    @Override
    public RespResult findAll() {
        return null;
    }

    @Override
    public RespResult findByConditions() {
        return null;
    }

    @Override
    public RespResult findAllCustomers() {
        try {
            List<HashMap<String, Object>> customers = afterSaleMapper.selectAllCustomers();
            if (customers.size() > 0) {
                return RespResult.success(customers);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return RespResult.error("哦豁，服务器崩了");
        }
        return RespResult.success("没有查到数据哦");
    }

    public RespResult upload(HttpServletRequest request) throws Exception {
        /*=====================1.创建目录=======================*/
        //6.自动获取项目路径的根路径，然后拼接上给的路径
        //String baseDir = request.getServletContext().getRealPath("");
        String baseDir = "D:/files/";
        //7.判断该路径下是否有该文件，无就创建
        File fileTh = new File(baseDir);
        if (!fileTh.exists() && !fileTh.isDirectory()) {
            fileTh.mkdir();
        }
        /*=================2.获取文件并且拼接最终保存文件名========================*/
        List<Part> files = null;
        files = (List<Part>) request.getParts();
        List<String> url=new ArrayList<>();
        for (Part file : files) {
            //2、从文件中获取文件名
            String filename = file.getSubmittedFileName();
            //3.从文件名中获取拓展名（以.分割)
            String substring = filename.substring(filename.lastIndexOf("."));
            //4.生成唯一UUID防止文件名称相同发生覆盖
            String UUID = java.util.UUID.randomUUID().toString();
            //5.拼接最终保存的文件名
            String finalfilename = UUID + substring;
            /*===============根据遍历写入====================*/
            //8.最后将上传的文件写入对用位置（路径加最终文件名）
            file.write(baseDir + "/" + finalfilename);
            System.out.println(baseDir + "/" + finalfilename);
            //9.生成一个路径存到数据库中(此路径可以在浏览器中访问)

            String path = "http://localhost:8080/images/" + finalfilename;
            url.add(path);
            System.out.println("------------------------" + path);

        }
        return RespResult.success(url);
    }



        @Override
    public RespResult addAfterSaleInformation(HttpServletRequest request,Map<String,Object>map) {
        return null;
    }

    /*===============5、根据客户名查相关商品============*/
    @Override
    public RespResult findGoods(String customerName) {
        try {
            List<FindGoodsSaleVO> goodsSaleVOS=afterSaleMapper.selectByCustomerName(customerName);
            if (goodsSaleVOS.size()>0){
                return RespResult.success(goodsSaleVOS);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return RespResult.error("哦豁，服务器崩了");
        }
    return RespResult.success("没有查到数据哦");
    }

    @Override
    public RespResult findAfterSalesClassifications() {
        try {
                List classifications=afterSaleMapper.selectAfterSaleClassification();
            if (classifications.size()>0){
                return RespResult.success(classifications);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return RespResult.error("哦豁，服务器崩了");
        }
        return RespResult.success("没有查到数据哦");
    }

    @Override
    public RespResult addAfterSalesClassification(String insertAfterSaleClassification) {
        try {
            Integer row=afterSaleMapper.insertAfterSaleClassification(insertAfterSaleClassification);
            if (row>0){
                return RespResult.success(row);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return RespResult.error("哦豁，服务器崩了");
        }
        return RespResult.success("没有查到数据哦");
    }

    @Override
    public RespResult modifyAfterSalesClassification(String oldAfterSaleClassification,String updateAfterSaleClassification) {
        try {
            Integer row=afterSaleMapper.updateAfterSaleClassification(oldAfterSaleClassification,updateAfterSaleClassification);
            if (row>0){
                return RespResult.success(row);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return RespResult.error("哦豁，服务器崩了");
        }
        return RespResult.success("没有查到数据哦");
    }

    @Override
    public RespResult removAfterSalesClassification(String deleteAfterSaleClassification) {
        try {
            Integer row=afterSaleMapper.deleteAfterSaleClassification(deleteAfterSaleClassification);
            if (row>0){
                return RespResult.success(row);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return RespResult.error("哦豁，服务器崩了");
        }
        return RespResult.success("没有查到数据哦");
    }

    /*================8、查询联系人==================*/
    @Override
    public RespResult findContacts(Integer customerId) {
        try {
            List<HashMap> contactNameAndPhones=afterSaleMapper.selectContactNameAndPhoneByCustomerId(customerId);
            if (contactNameAndPhones.size()>0){
                return RespResult.success(contactNameAndPhones);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return RespResult.error("哦豁，服务器崩了");
        }
        return RespResult.success("没有查到数据哦");
    }

    @Override
    public RespResult addContact(Map<String,Object>map){
        try {
            Integer row=afterSaleMapper.insertContact(map);
            if (row>0){
                return RespResult.success("真棒,添加成功,呀呼~");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return RespResult.error("哦豁，服务器崩了");
        }
        return RespResult.success("没有查到数据哦");
    }

    @Override
    public RespResult findPosition() {
        try {
            List<String> positions=afterSaleMapper.selectPosition();
            if (positions.size()>0){
                return RespResult.success(positions);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return RespResult.error("哦豁，服务器崩了");
        }
        return RespResult.success("没有查到数据哦");
    }

    @Override
    public RespResult appendixDetails(Integer afterSaleId) {
        try {
            List<HashMap>appendixDetails=afterSaleMapper.selectAppendixDetails(afterSaleId);
            if (appendixDetails.size()>0){
                return RespResult.success(appendixDetails);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return RespResult.error("哦豁，服务器崩了");
        }
        return RespResult.success("没有查到数据哦");
    }

    @Override
    public RespResult remove(Integer afterSaleId,String deleteDescription) {
        try {
           Integer row=afterSaleMapper.deleteAfterSale(afterSaleId,deleteDescription);
            if (row>0){
                return RespResult.success(row);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return RespResult.error("哦豁，服务器崩了");
        }
        return RespResult.success("没有查到数据哦");
    }

    @Override
    public RespResult removeAppendix(String appendixName) {
        try {
            Integer  row=afterSaleMapper.deleteAppendix(appendixName);
            if (row>0){
                return RespResult.success(row);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return RespResult.error("哦豁，服务器崩了");
        }
        return RespResult.success("没有查到数据哦");
    }


    @Override
    public RespResult modify() {
        return null;
    }

    @Override
    public RespResult modifyStatus(Integer afterSaleId,Integer afterSalesStatus,String handlingTime,String handlingResult) {
        try {
            Integer  row=afterSaleMapper.updateByClick(afterSaleId,afterSalesStatus,handlingTime,handlingResult);
            if (row>0){
                return RespResult.success(row);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return RespResult.error("哦豁，服务器崩了");
        }
        return RespResult.success("没有查到数据哦");
    }

    @Override
    public RespResult findAfterSaleByContact() {
        return null;
    }

    @Override
    public RespResult findAfterSaleBypage() {
        return null;
    }
}
